package yawning.mew.cat;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.cat.CatModelSimState.CatTargetData;
import yawning.mew.cat.CatModelSimulation.SimulatorProperty;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationTarget;

public class ShredAction implements SimulationAction {
	CatModelSimState mState;
	private SimulationTarget mTarget;
	private String mTargetName;
	private CatTargetData mTargetData;
	private boolean mExternalBleed;

	ShredAction(CatModelSimState state, CatTargetData targetData) {
		mState = state;
		mTarget = targetData.getSimulationTarget();
		mTargetData = targetData;
		mTargetName = mTarget.getTarget().getName();

		mExternalBleed = mState.mSimEngine.getConfig().getBool(SimulatorProperty.ASSUMEEXTERNALBLEED);
	}

	public double perform() {
		int cost = mState.getEnergyCost(mState.mModel.shredCost);
		if (mState.getEnergy() < cost && !mState.mOocEv.isUp()) return SimulationAction.ACTION_FAIL;
		int prevEnergy = mState.getEnergy(); int prevCps = mState.getComboPoints();
		if (mTargetData.mFrontalAttacksOnly) return SimulationAction.ACTION_FAIL;

		// Technically speaking, Shred being parried is nonsensical, but this happens for mobs that have 240 degree rear arcs
		AttackResult result = (mTargetData.mCanParry) ? mState.mModel.getFrontalYellowResult() : mState.mModel.getYellowResult();

		if (result == AttackResult.MISS) {
			cost = (int) Math.round(cost * 0.2);
			mNrShredMiss++;

			if (mState.mIsLogging) mState.mLog.log("Shred: %s MISS\n", mTargetName);
		} else if (result == AttackResult.DODGE) {
			cost = (int) Math.round(cost * 0.2);
			mNrShredDodge++;

			if (mState.mIsLogging) mState.mLog.log("Shred: %s DODGE\n", mTargetName);
		} else if (result == AttackResult.PARRY) {
			cost = (int) Math.round(cost * 0.2);
			mNrShredParry++;

			if (mState.mIsLogging) mState.mLog.log("Shred: %s PARRY\n", mTargetName);
		} else {
			double damage = mState.mModel.getWeaponDamage() * mState.mModel.levelDep.getShredMult() + mState.mModel.levelDep.getShredDmg();

			if (result == AttackResult.CRIT) {
				mNrShredCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrShredHit++;
			}

			damage *= mState.getDirectDamageMult();
			if (mExternalBleed || mTargetData.mRakeAction.isUp() || mTargetData.mRipAction.isUp() || mTargetData.mPounceAction.isUp()) {
				damage *= mState.mModel.multShredRnT;
			}
			damage *= mTargetData.mMangleAction.isUp() ? mState.mModel.multMangle : 1;
			mShredDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("Shred: %s %s Damage: %d\n", mTargetName, result, Math.round(damage));

			if (result == AttackResult.CRIT) mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOWCRIT); else mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOW);

			// Handle Glyph of Bloodletting.
			if (mTargetData.mRipAction.isUp() && mState.mModel.ripGlyphExtDuration > 0) mTargetData.mRipAction.onGlyphTrigger();

			if (mState.mModel.druid.tier_12_2pc) mTargetData.mTier_12_2pcEv.run(damage);

			mState.onCpGenerator(result == AttackResult.CRIT);
		}

		if (mState.mOocEv.isUp()) mNrShredOoC++;
		if (mState.mTigersFuryEv.isUp()) mNrShredTF++;

		mShredEnergy += mState.onOoCConsumer(cost);

		mState.logResourceChange(prevEnergy, prevCps);

		return 1.0;
	}

	long mShredDamage;
	int mNrShredMiss;
	int mNrShredDodge;
	int mNrShredParry;
	int mNrShredCrit;
	int mNrShredHit;

	int mShredEnergy;
	int mNrShredOoC;
	int mNrShredTF;
};